﻿using System;
using System.ComponentModel;
using System.Windows;
using LukasKopenec.LocalizationSupport;
using Microsoft.Win32;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that inserts a new image section to the active page
    /// presenting an image that the user has selected from the file system.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The command can execute if the active page is not <see langword="null"/>.
    /// </para>
    /// <para>
    /// The execution proceeds as follows:
    /// <list type="number">
    /// <item>
    /// An Open File dialog is showed to the user (filter is set to image files).
    /// </item>
    /// <item>
    /// If the user confirms the dialog, the selected file is first inserted
    /// to the root resources directory (using the <see cref="ViewModel.NewResourceCommand"/>).
    /// </item>
    /// <item>
    /// Finally the current application state's mouse target is set to the active page control
    /// and the command to a <see cref="ViewModel.NewImageSectionCommand"/> instance
    /// whose ImageResource parameter is bound to the CreatedResource of the command
    /// from the previous step.
    /// </item>
    /// </list>
    /// </para>
    /// </remarks>
    public class InsertImageSectionFromFileCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="InsertImageSectionFromFileCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// Reference to the application's main window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public InsertImageSectionFromFileCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Insert an Image from a File";
            ScreenTipText = "Creates an image section in the current page that will display a selected picture from your file system.";
            DisableReason = "A page must be open first.";

            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var dlg = new OpenFileDialog();
            dlg.AddExtension = true;
            dlg.Filter = (string)LocalizationManager.GetValue("Application.FileFilters.ImageFiles");
            dlg.CheckFileExists = true;
            
            bool? result = dlg.ShowDialog();
            if (result == true)
            {
                var newResourceCommand = new ViewModel.NewResourceCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
                {
                    FileName = dlg.FileName,
                    Project = ApplicationWindow.ProjectExplorer.Projects[0]
                };
                newResourceCommand.Parent = newResourceCommand.Project.GetRootDirectory(ViewModel.Elements.RootDirectory.Resources);

                var cmd = new ViewModel.NewImageSectionCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
                {
                    Owner = ApplicationWindow.ActivePage
                };
                cmd.BindParameterToCommand("ImageResource", newResourceCommand, "CreatedResource");

                var appState = ((App)App.Current).CurrentState;
                appState.MouseTarget = typeof(Controls.Page);
                appState.Command = cmd;
                appState.StatusBarMessage = "Draw the area where you want to put the section.";
            }
        }

        #endregion       
 
        #region Event Handlers

        /// <summary>
        /// Occurs when a property of the main application window has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
                RaiseCanExecuteChanged();
        }

        #endregion
    }
}
